package whu.o2o.adapter;

import java.util.ArrayList;

import whu.o2o.vo.RouteVO;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import whu.o2o.R;

public class RouteAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mLayoutInflater;
	private ArrayList<RouteVO> mList;

	public RouteAdapter(Context context, ArrayList<RouteVO> list) {
		mContext = context;
		mList = list;
		mLayoutInflater = LayoutInflater.from(mContext);
	}

	public int getCount() {
		return mList.size();
	}

	public Object getItem(int position) {
		return mList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		RouteVO vo = mList.get(position);

		ViewHolder holder;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.listitem_route, null);

			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.introduction = (TextView) convertView.findViewById(R.id.introduction);
			holder.price = (TextView) convertView.findViewById(R.id.price);
			holder.mat = (TextView) convertView.findViewById(R.id.mat);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.title.setText(vo.getName());
		holder.introduction.setText(vo.getIntroduction());
		holder.price.setText(String.format(mContext.getResources().getString(R.string.price), vo.getPrice()));
		holder.mat.setText(String.format(mContext.getResources().getString(R.string.mat), vo.getReservedCount()));
		return convertView;
	}

	static class ViewHolder {
		ImageView image;
		TextView title;
		TextView introduction;
		TextView price;
		TextView mat;
	}
}
